#include "RenzoMustHave.h"
#include "RenzoPrecompiled.h"
#include "RenzoMesh.h"
#include "RenzoCamera.h"
#include "RenzoScene.h"
#include "RenzoSceneNode.h"
#include "RenzoRenderSystem.h"
#include "RenzoRenderBuffer.h"
#include "RenzoResourceManager.h"
#include "RenzoSceneManager.h"

namespace Renzo
{
	void rzTestRenzoderer_Kickoff(int argc, char* argv[])
	{		
		Renzoderer* rz = Renzoderer::instance();	
		ResourceManager* rm = rz->getResourceManager();
		SceneManager* sm = rz->getSceneManager();

		rz->setArguments(argc, argv);
		rz->setWindowSize(800, 600);
	#ifdef _DEBUG
		rz->loadPlugin("RenzodererGL_d.dll");
		rz->loadPlugin("RenzoFreeImage_d.dll");
		rz->loadPlugin("RenzoSceneIO_d.dll");
	#else
		rz->loadPlugin("RenzodererGL.dll");
		rz->loadPlugin("RenzoFreeImage.dll");
		rz->loadPlugin("RenzoSceneIO.dll");
	#endif
		// create a window
		// create a viewport

		// load a mesh
		SceneNode* tris			= sm->load("./data/mesh/tris.md2");	
		SceneNode* weapon		= sm->load("./data/mesh/weapon.md2");
		SceneNode* watertruck	= sm->load("./data/mesh/watertruck.md2");
		SceneNode* excavatorT	= sm->load("./data/mesh/excavator_t.md2");
		SceneNode* excavatorB	= sm->load("./data/mesh/excavator_b.md2");
		SceneNode* gazeboT		= sm->load("./data/mesh/gazebo_t.md2");
		SceneNode* gazeboB		= sm->load("./data/mesh/gazebo_b.md2");
		SceneNode* waterput		= sm->load("./data/mesh/waterput.md2");		
		Camera* camera = (Camera*)Renzoderer::instance()->getResourceManager()->createObject(CameraType);
		camera->setView(Float3(-20.0f, 30.0f, 150.0f), Float3(5.0f, 3.0f, 16.0f), Float3(0.0f, 1.0f, 0.0f));
		camera->setViewCenteredBox(excavatorT->getBoundingBoxAABB());
		
		Scene* s = new Scene;		
		SceneNode* sn;

		s->add(tris);	tris->setRotation(Float3(-90.0f, 0.0f, 0.0f)); // MD2 mesh orientation
		s->add(weapon); weapon->setRotation(Float3(-90.0f, 0.0f, 0.0f));
		s->add(watertruck); watertruck->setRotation(Float3(-90.0f, -90.0f, 0.0f)); watertruck->setScale(Float3(0.5f, 0.5f, 0.5f)); watertruck->setTranslation(Float3(0.0f, 0.0f, -50.0f));
		s->add(excavatorT); excavatorT->setRotation(Float3(-90.0f, -90.0f, 0.0f)); excavatorT->setScale(Float3(0.5f, 0.5f, 0.5f)); excavatorT->setTranslation(Float3(50.0f, 0.0f, -50.0f));
		s->add(excavatorB); excavatorB->setRotation(Float3(-90.0f, -90.0f, 0.0f)); excavatorB->setScale(Float3(0.5f, 0.5f, 0.5f)); excavatorB->setTranslation(Float3(50.0f, 0.0f, -50.0f));
		s->add(gazeboT); gazeboT->setRotation(Float3(-90.0f, -90.0f, 0.0f)); gazeboT->setScale(Float3(0.75f, 0.75f, 0.75f));
		s->add(gazeboB); gazeboB->setRotation(Float3(-90.0f, -90.0f, 0.0f)); gazeboB->setScale(Float3(0.75f, 0.75f, 0.75f));
		s->add(waterput); waterput->setRotation(Float3(-90.0f, -90.0f, 0.0f)); waterput->setScale(Float3(0.75f, 0.75f, 0.75f));		
		s->add(camera);
		s->setActiveCamera(camera);

		rz->addScene(s);
		rz->setActiveScene(s);
		
		// attach scene to viewport
		// attach viewport to window
		// show
		rz->kickoff();
	#ifdef _DEBUG
		rz->unloadPlugin("RenzodererGL_d.dll");
		rz->unloadPlugin("RenzoFreeImage_d.dll");
		rz->unloadPlugin("RenzoSceneIO_d.dll");
	#else
		rz->unloadPlugin("RenzodererGL.dll");
		rz->unloadPlugin("RenzoFreeImage.dll");
		rz->unloadPlugin("RenzoSceneIO.dll");
	#endif

		rm->disposeAll();
	}
}